const App = getApp();
// 工具类
//import Util from '../../../utils/util.js';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    teamId:0,
    cate:1,   // 1积分，2捐款
    list:[],
    switchChecked:false,  //只看本队排名
    myNumber:0,
    myRanking:0,
    page:1,// 当前页码
    last_page:1,
    scrollHeight: null,
    isScrollAble:true,
    no_more: false, // 没有更多数据
    isLoading: true, // 是否正在加载中
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.cate == 2){
      this.setData({
        cate:2
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this;
    that.setData({
      isLogin: App.checkIsLogin(),
      page:1,// 当前页码
      list:[],
      last_page:1,
      no_more: false, // 没有更多数据
      isLoading: true, // 是否正在加载中
    });
    if (that.data.isLogin) {
      //获取服务队
      that.setData({
        teamId: App.checkTeam()
      });
       //设置页面高度
       this.setListHeight();
      // 获取列表
      that.getDataList();
      //回到页面顶部
      if (wx.pageScrollTo) {
        wx.pageScrollTo({
            scrollTop: 0
        })
      }
    }
  },
  /**
   * 下拉到底加载数据
   */
  bindDownLoad() {
    // 已经是最后一页
    console.log("翻页");
    console.log("page =",this.data.page)
    console.log("last_page =",this.data.last_page)
    if (this.data.page >= this.data.last_page) {
      this.setData({
        no_more: true
      });
      return false;
    }
    // 加载下一页列表
    this.getDataList(true, ++this.data.page);
  },
  //获取数据列表
  getDataList(isPage, page){
    let that = this;
    let obj = {
      is_team:0,
      page: page || 1,
    }
    if(that.data.switchChecked){
      obj.is_team = 1;
    }
    //1积分
    if(that.data.cate == 1){
      App._get('user/pointsRanking', obj, result => {
        let resList = result.data,
        dataList = that.data.list;
        if (isPage == true) {
          that.setData({
            list: dataList.concat(resList.points_ranking),
            myNumber:result.data.user.points,
            myRanking:result.data.user.my_ranking,
            isLoading: false,
            last_page: resList.last_page,
          }); 
       }else {
          that.setData({
            list: resList.points_ranking,
            myNumber:resList.user.points,
            myRanking:resList.user.my_ranking,
            isLoading: false,
            last_page: resList.last_page,
          });
      }

      });
    }
    //2捐款
    if(that.data.cate == 2){
      App._get('user/donateRanking', obj, result => {
        let resList = result.data,
          dataList = that.data.list;
        if (isPage == true) {
          that.setData({
            list: dataList.concat(resList.donate_ranking),
            myNumber:resList.user.donate,
            myRanking:resList.user.my_ranking,
            isLoading: false,
            last_page: resList.last_page,
          });
        } else {
          that.setData({
            list: resList.donate_ranking,
            myNumber:resList.user.donate,
            myRanking:resList.user.my_ranking,
            isLoading: false,
            last_page: resList.last_page,
          });
        }

      });
    }
    
  },
  //只看本队switch
  switchChange(e){
    let that = this
    this.setData({
      switchChecked:e.detail.value
    })
    that.getDataList();
   
  },
  // tab切换
  onChangeNews(e){
    let that = this;
    that.setData({
      cate: e.currentTarget.dataset.id,
      list: [],
      page: 1,
      last_page:1,
      no_more: false,
      isLoading: true,
    }); 
    that.getDataList();
  },
  /**
   * 设置文章列表高度
   */
  setListHeight() {
    let systemInfo = wx.getSystemInfoSync(),
      rpx = systemInfo.windowWidth / 750, // 计算rpx
      tapHeight = Math.floor(rpx * 78), // tap高度
      scrollHeight = systemInfo.windowHeight - tapHeight; // swiper高度
      console.log(systemInfo.windowHeight,scrollHeight);
      this.setData({
        scrollHeight
      });
  },

})